const artical = require('../models/artical')
const Sequelize = require('sequelize');
const Op = Sequelize.Op;

class articalDao {
  // 新增
  static async create(v) {
    const res = await artical.create(
      v
    )
    return res
  }
  // 获取单条信息
  static async findById(v) {
    const res = await artical.findOne({
      where: {
        id: v
      },
    });
    return res
  }
  // 删除单条信息
  static async delById(v) {
    const res = await artical.destroy({
      where: {
        id: v
      }
    })
    return res
  }
  // 获取所有数据(分页)
  static async findAll(v) {
    const res = await artical.findAndCountAll({
      limit: v.limit,
      offset: v.offset
    })
    let result = {}
    result.data = res.rows;
    result.total = res.count;
    return result
  }
  // 新增浏览
  static async addPv(v) {
    const res = await artical.increment(['pv'], {
      where: {
        id: v
      }
    });
    return res
  }
  // 获取精选文章
  static async getMostPV(v) {
    let limit = 4;
    let offset = 0;
    const res = await artical.findAll({
      order: [
        ['pv', 'DESC']
      ],
      limit: limit,
      offset: offset
    })
    return res
  }
  // 获取推荐
  static async findRand(v) {
    let limit = 5;
    let offset = 0;
    const res = await artical.findAll({
      order: [
        ['pv', 'DESC']
      ],
      limit: limit,
      offset: offset
    });
    return res
  }
  // 通过审核
  static async pass(v) {
    const res = await artical.update(
      {
        status:1
      },
      {
      where: {
        id: v
      }
    })
    return res
  }
}

module.exports = articalDao